var DEFAULT_HOMEPAGE_BANNERS = [
  {
    image_zh: `${CDN_PREFIX}/assets/images/featured/2020banner_video/mooncake-banner.jpg`,
    image_en: `${CDN_PREFIX}/assets/images/featured/2020banner_video/mooncake-banner.jpg`,
    redirectUrl: 'menu/food/mooncake-mobile/',
    extra: {
      h5_image_zh: `${CDN_PREFIX}/assets/images/featured/2020banner_video/mooncake-banner-mobile.jpg`,
      h5_image_en: `${CDN_PREFIX}/assets/images/featured/2020banner_video/mooncake-banner-mobile.jpg`
    }
  },
  {
    image_zh: `${CDN_PREFIX}/assets/images/featured/2020autumn2/qia-yi-bei.jpg`,
    image_en: `${CDN_PREFIX}/assets/images/featured/2020autumn2/qia-yi-bei.jpg`,
    redirectUrl: 'menu/',
    extra: {
      h5_image_zh: `${CDN_PREFIX}/assets/images/featured/2020autumn2/qia-yi-bei-mobile.jpg`,
      h5_image_en: `${CDN_PREFIX}/assets/images/featured/2020autumn2/qia-yi-bei-mobile.jpg`
    }
  },
  {
    image_zh: `${CDN_PREFIX}/assets/images/featured/club_upgrade/banner.jpg`,
    image_en: `${CDN_PREFIX}/assets/images/featured/club_upgrade/banner.jpg`,
    redirectUrl: 'account/starbucks-rewards/',
    extra: {
      h5_image_zh: `${CDN_PREFIX}/assets/images/featured/club_upgrade/banner_mobile.jpg`,
      h5_image_en: `${CDN_PREFIX}/assets/images/featured/club_upgrade/banner_mobile.jpg`
    }
  }
];
var BANNER_CACHE_HOMEPAGE = 'BANNER_CACHE_HOMEPAGE';

function handleHomePageBannerRes(result) {
  if(result && result.length > 0) {
    let bannersHtml = '';
    let homepageBanners = [];
    result.forEach(item => {
      let banner = {
        href: item.redirectUrl,
        src: LOCALE === 'zh' ? item.image_zh : item.image_en,
        src_mobile: LOCALE === 'zh' ? item.extra.h5_image_zh : item.extra.h5_image_en
      };
      homepageBanners.push(banner);
    });
    bannersHtml = generateBannersHtml(homepageBanners);
    $('.carousel-home').html(bannersHtml);
    console.log('生成html');
    return;
  }
}

function setHomepageBanner() {
  getBannerConfig('homepage-banner', BANNER_CACHE_HOMEPAGE, DEFAULT_HOMEPAGE_BANNERS, handleHomePageBannerRes);
}

function generateBannersHtml(bannersConfigData) {
  var bannersCount = bannersConfigData.length;
  var generatedHtml = '';
  if (bannersCount > 0) {
    bannersConfigData.forEach(banner => {
      if(banner.href) {
        generatedHtml += `
          <div>
            <a class='block relative' href='${banner.href}'>
              <img src='${banner.src}' class='carousel-image tablet-block mobile-hidden' />
              <img src='${banner.src_mobile}' class='mobile-carousel-image show-mobile tablet-hidden' />
            </a>
          </div>
        `;
      }
      else {
        generatedHtml += `
          <div>
            <span class='block relative'>
              <img src='${banner.src}' class='carousel-image tablet-block mobile-hidden' />
              <img src='${banner.src_mobile}' class='mobile-carousel-image show-mobile tablet-hidden' />
            </span>
          </div>
        `;
      }
    });
  }
  return generatedHtml;
}

setHomepageBanner();